<?php

require("includes/config.php");
require("includes/function.common.php");

#################################################################################
# State Format

# Player Array (
#   Player ID
#	Identity
#   Array of Message
#   Array of Hand
# )
# Cards On Desk
# Round, Turn

// This PHP is to be called every 1 second (For Example)
// to retrieve the state of the game.

##################################################################################
# GLOBAL Variables

$reply		 = array();
$playerInfo	 = array();
$desk		 = array();
$game_states = array();
$display_message;

#
##################################################################################

##################################################################################
# Function

function retrieveState()
{
	global $debug, $dsn, $dbUser, $dbPW, $dbPW, $database, $sth;
	global $reply, $playerInfo, $desk, $game_states, $display_message;
	
	try
	{
		$database = new PDO($dsn, $dbUser, $dbPW);
		$database->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
	}
	catch (PDOException $e)
	{
		echo 'Connection failed: ' . $e->getMessage();
	}
	
	##################################################################################
	# Find Player Number
	
	$query = "SELECT MAX(p.player) FROM players p";
	$sth = $database->prepare($query);
	
	try
	{
		$sth->execute();
		if ( $result = $sth->fetch() )
		{
			for ($i = 0; $i < $result[0]; $i++)
			{
				$playerInfo[$i] = Array($i + 1, 0, Array(), Array());
			}
		}
	}
	catch (PDOException $e)
	{
		print 'Exception : ' . $e->getMessaage();
	}
	
	##################################################################################
	# Player's Identity
	
	$query = 'SELECT * FROM players';
	$sth = $database->prepare($query);
	
	if ($sth == null) echo "sth is null";
	
	try
	{
		$sth->execute();
		while ($result = $sth->fetch())
		{
			$temp_player = (int) $result[0];
			$temp_identity = (int) $result[1];
			
			$playerInfo[$temp_player - 1][1] = $temp_identity;
		}
	}
	catch (PDOException $e)
	{
		echo 'Query Identity failed: ' . $e->getMessage();
	}
	
	##################################################################################
	# All Players' Message Card
	
	$query = "SELECT c.sequence, c.cid FROM cards c WHERE c.place = 5 ORDER BY c.sequence, c.cid";
	$sth = $database->prepare($query);
	
	if ($sth == null) echo "sth is null";
	
	try
	{
		$sth->execute();
		
		while ($result = $sth->fetch())
		{
			$temp_player = (int) $result[0];
			$temp_cid = (int) $result[1];
			
			array_push($playerInfo[$temp_player - 1][2], $temp_cid);
		}
	}
	catch (PDOException $e)
	{
		print 'Players\' Message Card, Exception : ' . $e->getMessaage();
	}
	
	##################################################################################
	# All Players' Hand Card
	
	$query = "SELECT c.sequence, c.cid FROM cards c WHERE c.place = 1 ORDER BY c.sequence, c.cid";
	$sth = $database->prepare($query);
	
	if ($sth == null) echo "sth is null";
	
	try
	{
		$sth->execute();
		while ($result = $sth->fetch())
		{
			$temp_player = (int) $result[0];
			$temp_cid = (int) $result[1];
			
			array_push($playerInfo[$temp_player - 1][3], $temp_cid);
		}
	}
	catch (PDOException $e)
	{
		echo 'Query Hand Card, Exception: ' . $e->getMessage();
	}
	
	$reply = array('player'=>$playerInfo);
	
	##################################################################################
	# Desk's Card
	
	$query = "SELECT * FROM cards c WHERE c.place = 7 ORDER BY c.sequence";
	$sth = $database->prepare($query);
	
	if ($sth == null) echo "sth is null";
	
	try
	{
		$sth->execute();
		while ( $result = $sth->fetch() )
		{
			array_push($desk, (int)$result[0]);
		}
	}
	catch (PDOException $e)
	{
		print 'Exception : ' . $e->getMessaage();
	}
	
	$reply['desk'] = $desk;
	
	
	##################################################################################
	# Turn and Priority
	
	$query = "SELECT * FROM states s";
	$sth = $database->prepare($query);
	
	if ($sth == null) echo "sth is null";
	
	try
	{
		$sth->execute();
		if ( $result = $sth->fetch() )
		{
			$game_states[0] = (int) $result[0];
			$game_states[1] = (int) $result[1];
			$game_states[2] = (int) $result[2];
			$game_states[3] = (int) $result[3];
		}
	}
	catch (PDOException $e)
	{
		print 'Exception : ' . $e->getMessaage();
	}
	
	$reply['game_states'] = $game_states;
	
	##################################################################################
	# Game State Message
	
	switch ($game_states[0])
	{
	case 0:
		// Initialization
		$display_message = "Initializing";
		break;
	case 1:
		$display_message = "Game Start Phase";
		break;
	case 2:
		$display_message = "Draw Card Phase";
		break;
	case 3:
		if ($game_states[1] == 0)
		{
			$display_message = "Enter Play Card Phase 1";
		}
		else if ($game_states[1] == 1)
		{
			$display_message = "Player plays a Card";
		}
		break;
	case 4:
		if ($game_states[1] == 0)
		{
			$display_message = "Enter Message Transmit Phase";
		}
		else if ($game_states[1] == 1)
		{
			$display_message = "Message Transmit";
		}
		else if ($game_states[1] == 2)
		{
			$display_message = "Message Receive";
		}
		else if ($game_states[1] == 3)
		{
			$display_message = "Message Confirm";
		}
		break;
	case 5:
		if ($game_states[1] == 0)
		{
			$display_message = "Enter Play Card Phase 1";
		}
		else if ($game_states[1] == 1)
		{
			$display_message = "Player plays a Card";
		}
		break;
	case 6:
		$display_message = "Game End Phase";
		break;
	default:
		echo "Unknown State Encountered! =0= <br />";
	}
	
	$reply['display_message'] = $display_message;
	
	##################################################################################
	# Check if json is the same as before
	
	$current_json = json_encode($reply);
	$last_json;
	
	$query = "SELECT * FROM last_json";
	$sth = $database->prepare($query);
	
	try
	{
		$sth->execute();
		
		if ($result = $sth->fetch())
		{
			$last_json = $result[0];
		}
	}
	catch (PDOException $e)
	{
		echo 'Query last_json failed, Exception: ' . $e->getMessage();
	}
	
	if ($last_json == $current_json)
	{
		echo 'not modified';
	}
	else
	{
		$query = "UPDATE last_json SET json = '$current_json'";
		$sth = $database->prepare($query);
		
		try
		{
			if ( !($result = $sth->execute()) )
			{
				echo 'Update last_json failed';
			}
			else
			{
				echo $current_json;
			}
		}
		catch (PDOException $e)
		{
			echo 'Query last_json failed, Exception: ' . $e->getMessage();
		}
	}
}
?>